package com.lkd.job;

import com.lkd.common.VMSystem;
import com.lkd.entity.UserEntity;
import com.lkd.service.UserService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class UserXxlJob {
    @Autowired
    private RedisTemplate redisTemplate;
  @Autowired
  private UserService userService;

    @XxlJob("workCountInitJobHandler")
    public void initRedis() {
        //生成大key
        String data = LocalDate.now().plusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));

        List<UserEntity> list = userService.list();

        list.stream().
                forEach(userEntity ->
                {
                    if (userEntity != null) {
                        String key = "lkd-user-service-redis" + data + userEntity.getRegionId() + userEntity.getRoleCode();
                        String sKey = userEntity.getId() + "";
                        redisTemplate.opsForZSet().add(key, sKey, 0);
                        redisTemplate.expire(key, 2, TimeUnit.DAYS);
                    }
                }
        );
    }
}
